package com.ibm.datamodel.exp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;


class Field{
	
	String name;
	String daatype = "double";
	boolean isFix = true;
	int dimension;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDaatype() {
		return daatype;
	}
	public void setDaatype(String daatype) {
		this.daatype = daatype;
	}
	public boolean isFix() {
		return isFix;
	}
	public void setFix(boolean isFix) {
		this.isFix = isFix;
	}
	public int getDimension() {
		return dimension;
	}
	public void setDimension(int dimension) {
		this.dimension = dimension;
	}
	
}


public class LongSchema {
	
//	List<Field> fields;
	Field[] fields;
//	List<Field> fields;
	HashMap<String, Field> fieldsMap;
	
	HashMap<String, Integer> name2index;
	
	boolean isFix;
	
	public void constructn2index(){
		int index = 0;
		for(Field f : fields){
			name2index.put(f.getName(), index);
			index += f.getDimension();
		}

	}
	
	public LongSchema(int[] die, boolean fix) {
//		fields = new ArrayList<>();
		fields = new Field[die.length];
		fieldsMap = new HashMap<>();
		name2index = new HashMap<>();
		if(fix){
			// allfix
			for (int i = 0; i < die.length; i++) {
				String name  = "tag_" + i;
				Field f = new Field();
				f.setName(name);
				f.setDimension(die[i]);
				f.setFix(true);
//				fields.add(f);
				fields[i]  = f;
				fieldsMap.put(name, f);
			}
		}else {
			// not fix
			for (int i = 0; i < die.length; i++) {
				String name  = "tag_" + i;
				Field f = new Field();
				f.setName(name);
				f.setFix(false);
				fields[i]  = f;
				fieldsMap.put(name, f);
			}
		}
		
		this.isFix = isAllFix();
		
	}
	
	
	public void constructn2index(CompareMessage message){
		// we assume the field has been sorted.
		int index = 0;
		for(Field f : fields){
			name2index.put(f.getName(), index);
			if (f.isFix()) {
				index += f.getDimension();
			}else {
				Comparable x = message.getValues()[index];
				index += (int)x;
				f.setDimension((int)x);
				index += 1;
			}
		}		

	}
	
	
	public boolean isAllFix(){
		boolean isFix = true;
		for(Field f : fields){
			if (!f.isFix()) {
				isFix = false;
				return isFix;
			}
		}
		return isFix;
	}
}
